<template>
  <div class="welcome-component">
    <el-card class="welcome-card" :class="{ 'edit-mode': isEditMode }">
      <!-- 编辑模式工具栏 -->
      <div v-if="isEditMode" class="component-toolbar">
        <el-button-group size="small">
          <el-button @click="$emit('configure-component', componentConfig)" icon="Setting" circle />
          <el-button @click="$emit('toggle-visibility')" :icon="component.visible ? 'View' : 'Hide'" circle />
          <el-button @click="$emit('remove-component')" icon="Delete" type="danger" circle />
        </el-button-group>
      </div>

      <!-- 欢迎内容 -->
      <div class="welcome-content">
        <div class="welcome-header">
          <div class="user-info">
            <el-avatar :size="60" :src="userAvatar" class="user-avatar">
              <el-icon><User /></el-icon>
            </el-avatar>
            <div class="user-details">
              <h2 class="welcome-title">{{ welcomeMessage }}</h2>
              <p class="welcome-subtitle">{{ currentTime }}</p>
            </div>
          </div>
          <div class="weather-info" v-if="config.showStats">
            <el-icon class="weather-icon"><Sunny /></el-icon>
            <span>{{ weather }}</span>
          </div>
        </div>

        <!-- 统计数据 -->
        <div v-if="config.showStats" class="stats-grid">
          <div class="stat-item" v-for="stat in stats" :key="stat.key">
            <div class="stat-icon">
              <el-icon :class="stat.iconClass">
                <component :is="stat.icon" />
              </el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ stat.value }}</div>
              <div class="stat-label">{{ stat.label }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { 
  User, 
  Sunny, 
  TrendCharts, 
  ShoppingCart, 
  Wallet, 
  UserFilled 
} from '@element-plus/icons-vue'

const props = defineProps({
  componentId: {
    type: String,
    required: true
  },
  componentConfig: {
    type: Object,
    default: () => ({})
  },
  component: {
    type: Object,
    required: true
  },
  isEditMode: {
    type: Boolean,
    default: false
  },
  visible: {
    type: Boolean,
    default: true
  }
})

defineEmits(['toggle-visibility', 'remove-component', 'configure-component'])

const userStore = useUserStore()
const currentTime = ref('')
const weather = ref('晴朗 25°C')

// 计算属性
const config = computed(() => props.component.config || {})
const userAvatar = computed(() => userStore.userInfo?.avatar || '')
const welcomeMessage = computed(() => {
  const hour = new Date().getHours()
  const name = userStore.userInfo?.name || '用户'
  if (hour < 12) return `早上好，${name}！`
  if (hour < 18) return `下午好，${name}！`
  return `晚上好，${name}！`
})

// 统计数据
const stats = ref([
  {
    key: 'sales',
    icon: TrendCharts,
    iconClass: 'text-blue-500',
    value: '¥128,456',
    label: '今日销售额'
  },
  {
    key: 'orders',
    icon: ShoppingCart,
    iconClass: 'text-green-500',
    value: '234',
    label: '今日订单'
  },
  {
    key: 'revenue',
    icon: Wallet,
    iconClass: 'text-orange-500',
    value: '¥45,678',
    label: '今日利润'
  },
  {
    key: 'customers',
    icon: UserFilled,
    iconClass: 'text-purple-500',
    value: '1,234',
    label: '活跃用户'
  }
])

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  updateTime()
  setInterval(updateTime, 60000) // 每分钟更新一次
})
</script>

<style scoped>
.welcome-component {
  position: relative;
}

.welcome-card {
  border-radius: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  transition: all 0.3s ease;
}

.welcome-card.edit-mode {
  border: 2px dashed #409eff;
  position: relative;
}

.welcome-card.edit-mode:hover {
  border-color: #67c23a;
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
}

.component-toolbar {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 6px;
  padding: 4px;
}

.welcome-content {
  padding: 20px;
}

.welcome-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-avatar {
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.user-details h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.user-details p {
  margin: 4px 0 0 0;
  opacity: 0.9;
  font-size: 14px;
}

.weather-info {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.2);
  padding: 8px 12px;
  border-radius: 20px;
  font-size: 14px;
}

.weather-icon {
  color: #ffd700;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255, 255, 255, 0.1);
  padding: 16px;
  border-radius: 8px;
  backdrop-filter: blur(10px);
}

.stat-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 2px;
}

.stat-label {
  font-size: 12px;
  opacity: 0.8;
}

@media (max-width: 768px) {
  .welcome-header {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .stat-item {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }
}
</style>